//
//  DetailViewController.m
//  Delicious
//
//  Created by lanou on 16/5/7.
//  Copyright © 2016年 gy. All rights reserved.
//

#import "DetailViewController.h"

@interface DetailViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIImageView *zoomImageView;
@property (nonatomic, strong) NSMutableArray *detailArr;
@property (nonatomic, strong) NSString *URLString;
@property (nonatomic, strong) NSMutableArray *firstArr;
@property (nonatomic, strong) NSMutableArray *materialArr;
@property (nonatomic, strong) NSDictionary *dict;
@property (nonatomic, strong) NSDictionary *personDict;
@property (nonatomic, assign) CGFloat f;
@property (nonatomic, assign) CGRect rect;
@property (nonatomic, assign) CGRect rectFoot;
@property (nonatomic, strong) UIImageView *footImage;
@end

@implementation DetailViewController
- (NSMutableArray *)detailArr
{
    if (!_detailArr) {
        _detailArr = [NSMutableArray array];
    }
    return _detailArr;
}
- (NSMutableArray *)firstArr
{
    if (!_firstArr) {
        _firstArr = [NSMutableArray array];
    }
    return _firstArr;
}
- (NSMutableArray *)materialArr
{
    if (!_materialArr) {
        _materialArr = [NSMutableArray array];
    }
    return _materialArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor purpleColor];
    
    [self getData];
    
   
    
    
}
- (void)creatTable
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64) style:UITableViewStylePlain];
    //3.设置代理，头文件也要包含 UITableViewDelegate,UITableViewDataSource
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self setImageUrl:self.URLString];
    UIView *oneView = [[UIView alloc] init];
    self.rect = CGRectMake(0, 64, kScreenWidth, IMAGEHEIGHT);
    oneView.frame = CGRectMake(0, 64, kScreenWidth, IMAGEHEIGHT);
    oneView.backgroundColor = [UIColor clearColor];
    self.tableView.tableHeaderView = oneView;
    self.tableView.tableHeaderView.backgroundColor = [UIColor clearColor];
    self.tableView.backgroundColor = [UIColor clearColor];
    
    // 底视图
//    [self setFootImage:self.URLString];
    UIView *twoView = [[UIView alloc] init];
    self.rectFoot = CGRectMake(0, kScreenHeight - IMAGEHEIGHT, kScreenWidth, IMAGEHEIGHT);
    twoView.frame = CGRectMake(0, kScreenHeight - IMAGEHEIGHT, kScreenWidth, IMAGEHEIGHT);
    twoView.backgroundColor = [UIColor clearColor];
    self.tableView.tableFooterView = twoView;
    self.tableView.tableFooterView.backgroundColor = [UIColor clearColor];
    self.tableView.backgroundColor = [UIColor clearColor];
    
    
    [self.view addSubview:self.tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"FirstTableViewCell" bundle:nil] forCellReuseIdentifier:@"first"];
    [self.tableView registerNib:[UINib nibWithNibName:@"MaterialTableViewCell" bundle:nil] forCellReuseIdentifier:@"material"];
    [self.tableView registerNib:[UINib nibWithNibName:@"DetailTableViewCell" bundle:nil] forCellReuseIdentifier:@"detail"];
    [self.tableView registerNib:[UINib nibWithNibName:@"PersonTableViewCell" bundle:nil] forCellReuseIdentifier:@"person"];
    
    
    

}

- (void)getData
{
    NSString *URLString = [ChuanDishDetail stringByAppendingString:self.ID];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    [manager GET:URLString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        NSLog(@"--%@", downloadProgress);
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dictionary = responseObject;
        self.dict = [NSDictionary dictionary];
        self.dict = dictionary[@"data"];
        self.URLString = self.dict[@"imageUrl"];
        self.personDict = self.dict[@"referrer"];
        NSArray *arr = self.dict[@"ingredients"];
        for (NSDictionary *materialDict in arr) {
            materialModel *model = [[materialModel alloc] init];
            [model setValuesForKeysWithDictionary:materialDict];
            [self.materialArr addObject:model];
            
    }
        
        NSArray *stepArr = self.dict[@"makingSteps"];
        for (NSDictionary *dict in stepArr) {
            DetailModel *model = [[DetailModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [self.detailArr addObject:model];
            
}
        [self creatTable];
        [self addNavigationButton]; // 添加导航栏控件
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"-+%@", error);
        
    }];
    
    
    
    
    
}




-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGPoint point = scrollView.contentOffset;
    if (point.y >= 0 &&point.y < IMAGEHEIGHT) {
       
        return;
    } else if (point.y < 0) {
    self.zoomImageView.frame = CGRectMake(self.rect.origin.x + point.y, self.rect.origin.y, self.rect.size.width - point.y * 2, self.rect.size.height - point.y * 2);
    } else if (point.y > IMAGEHEIGHT && point.y < IMAGEHEIGHT + 200){
        self.zoomImageView.frame = self.rect;
    } else if (point.y >= IMAGEHEIGHT + 200 && point.y < IMAGEHEIGHT + 300){
        self.zoomImageView.frame = CGRectMake(0, kScreenHeight - IMAGEHEIGHT, kScreenWidth, IMAGEHEIGHT);
        self.rectFoot = self.zoomImageView.frame;
    } else if (self.tableView.contentOffset.y + self.tableView.frame.size.height >= self.tableView.contentSize.height) {
        
        CGFloat a = self.tableView.contentOffset.y + self.tableView.frame.size.height - self.tableView.contentSize.height;
        self.zoomImageView.frame = CGRectMake(self.rectFoot.origin.x - a, self.rectFoot.origin.y - a, self.rectFoot.size.width + a * 2, self.rectFoot.size.height + a * 2);
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2 + self.materialArr.count + self.detailArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        PersonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"person" forIndexPath:indexPath];
        [cell.personImage sd_setImageWithURL:[NSURL URLWithString:self.personDict[@"profileImageUrl"]]];
        cell.name.text = self.personDict[@"nickname"];
        cell.Description.text = self.dict[@"description"];
       self.f = [cell heightForText:self.dict[@"description"]];
        
        return cell;
    } else if (indexPath.row == 1) {
        FirstTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"first" forIndexPath:indexPath];
        
    cell.difficulty.text = self.dict[@"difficulty"];
    cell.times.text = self.dict[@"times"];
    cell.taste.text = self.dict[@"taste"];
        
        return cell;
    } else if (indexPath.row >0 && indexPath.row < self.materialArr.count + 2) {
        MaterialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"material" forIndexPath:indexPath];
        materialModel *model = [self.materialArr objectAtIndex:indexPath.row - 2];
        [cell setMaterial:model];
        

        
        return cell;
    } else {
        DetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detail" forIndexPath:indexPath];
        
        
        
        DetailModel *model = [self.detailArr objectAtIndex:indexPath.row - self.materialArr.count - 2];
        [cell setDetail:model];
        [cell.imageUrl sd_setImageWithURL:[NSURL URLWithString:model.imageUrl]];

        return cell;
    }

    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row ==0 ) {
        return self.f + 80;
    } else if (indexPath.row == 1) {
        return 109;
    } else if(indexPath.row >1 && indexPath.row < self.materialArr.count + 2) {
        return 30;
    } else {
    return 145;
    }
    
}

- (void)setFootImage:(NSString *)str
{
    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]];
    UIImage *image = [UIImage imageWithData:data];
    _footImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, kScreenHeight - IMAGEHEIGHT, kScreenWidth, IMAGEHEIGHT)];
    _footImage.image = image;
    [self.view addSubview:_footImage];
}

- (void)setImageUrl:(NSString *)str
{
    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]];
   UIImage *image = [UIImage imageWithData:data];
    _zoomImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, IMAGEHEIGHT)];
    _zoomImageView.image = image;
    [self.view addSubview:_zoomImageView];

}

- (void)addNavigationButton
{
    UIBarButtonItem *share = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"share"] style:UIBarButtonItemStyleBordered target:self action:@selector(shareAction)];
    UIBarButtonItem *collection = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"collection"] style:UIBarButtonItemStyleBordered target:self action:@selector(collectionAction)];
    UIBarButtonItem *comment = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"comment"] style:UIBarButtonItemStyleBordered target:self action:@selector(commentAction)];
    
    // 如果是轮播图跳转过来的，不添加按钮
    
        
    self.navigationItem.rightBarButtonItems = @[share,collection,comment];
    
    
}
- (void)collection
{
    
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
